package com.example.demo2.student;


import com.example.demo2.Util.DBHelper;
import com.example.demo2.pojo.class1;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * <p>
 *
 * </p>
 *
 * @author rzw
 * @date 2023/12/14
 * @Version: 1.0
 */
@WebServlet("/FindClasssById")
public class FindClasssById extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public FindClasssById() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        class1 class1 = new class1();
        DBHelper dbHelper = new DBHelper();
        conn = dbHelper.getConn();
        int id = Integer.parseInt(request.getParameter("id"));
        try {
            preparedStatement = conn.prepareStatement("select *from webdesign.class where id =?");
            preparedStatement.setInt(1, id);
            rs = preparedStatement.executeQuery();
            while (rs.next()) {
                class1.setId(rs.getInt("id"));
                class1.setClassName(rs.getString("className"));

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            dbHelper.closeAll(conn,preparedStatement,rs);
        }
        request.setAttribute("classs", class1);
        request.getRequestDispatcher("/upclasss.jsp").forward(request, response);

    }

}
